package leilei.jace.com.tcpdemo2.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.widget.Toast;

import java.net.InetSocketAddress;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.timeout.IdleStateHandler;
import leilei.jace.com.tcpdemo2.listener.onServiceNotifiction;
import leilei.jace.com.tcpdemo2.text.ConstantValue;
import leilei.jace.com.tcpdemo2.util.SmartCarDecoder;
import leilei.jace.com.tcpdemo2.util.SmartCarEncoder;
import leilei.jace.com.tcpdemo2.util.ToastUtil;

/**
 * Created by Administrator on 2018/3/18 0018.
 */

public class MyService extends Service {
//    private ExecutorService executorService = Executors.newFixedThreadPool(10);

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
//        executorService.execute(new Runnable() {
//            @Override
//            public void run() {
//                try {
//                    start();
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//                TcpService.getInstance().StartTcpService();
//            }
//        });
        TcpService.getInstance().StartTcpService();
        ToastUtil.showShortToast("开启服务器");
    }


//    private void start() throws Exception {
////        final EchoServerHandler serverHandler = new EchoServerHandler(MyService.this);
//        EventLoopGroup bossGroup = new NioEventLoopGroup();
//        EventLoopGroup workerGroup = new NioEventLoopGroup();
//        try {
//            ServerBootstrap b = new ServerBootstrap();
//            b.group(bossGroup, workerGroup)
//                    .channel(NioServerSocketChannel.class)
//                    .handler(new LoggingHandler(LogLevel.INFO))
//                    .localAddress(new InetSocketAddress(ConstantValue.TCP_PORT))
//                    .childHandler(new ChannelInitializer<SocketChannel>() {
//                        @Override
//                        protected void initChannel(io.netty.channel.socket.SocketChannel ch) throws Exception {
//                            ch.pipeline().addLast(new IdleStateHandler(5, 0, 0, TimeUnit.SECONDS));
//                            ch.pipeline().addLast(new SmartCarEncoder());
//                            ch.pipeline().addLast(new SmartCarDecoder());
//                            ch.pipeline().addLast(new ServerHandler());
//                        }
//                    });
//            ChannelFuture f = b.bind().sync();
//            if (f.isSuccess()) {
////                serverSocketChannel = (java.nio.channels.ServerSocketChannel) f.channel();
////                ToastUtil.showShortToast(MyService.this, "连接成功");
//            } else {
////                ToastUtil.showShortToast(MyService.this, "连接失败");
//            }
//            f.channel().closeFuture().sync();
//        } catch (Exception e) {
//            e.printStackTrace();
//        } finally {
//            bossGroup.shutdownGracefully();
//            workerGroup.shutdownGracefully();
//        }
//    }
}
